/**
 * Created by jimmy on 17/2/26.
 */

// var Base = require('../../utils/base.js').base;
import { Base } from '../../utils/base.js';
import { Config } from '../../utils/config.js';

class Home extends Base {
  constructor() {
    super();
  }

  /*首页信息*/
  getAddress(district, callback) {
    var url = 'Api/Index/getAddress/district/' + district;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }


  /*分类信息*/
  getCateData(callback) {
    var that = this;
    var param = {
      url: 'Api/Index/Cate',

      sCallback: function (data) {
        data = data.items;
        callback && callback(data);
      }
    };
    this.request(param);
  }
  /*首页信息*/
  getInfoData(cate,district_id,p,callback) {
    var url = (cate == 0) ? 'Api/Index/Info' : 'Api/Index/Info/cate/'+cate;
    url = url + '/district_id/' + district_id+'/p/'+p;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  getSearch(data,callback) {
    var param = {
      url: "Api/Index/Info",
      data: data,
      type: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  /*置顶信息*/
  getFeeTypeData(callback) {
    var url = 'Api/Index/feeType';
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  /*信息详情*/
  getInfoDetail(id,callback) {
    var url = 'Api/Index/detail/id/' + id;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  //发布信息
  postSaveInfo(data, callback){
    var param = {
      url: 'Api/Index/saveInfo',
      data: data,
      type:'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  //发布评论
  submitComment(data, callback) {
    var param = {
      url: 'Api/Index/submitComment',
      data: data,
      type: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  submitAdopt(data, callback) {
    var param = {
      url: 'Api/Index/Adopt',
      data: data,
      type: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  //提现
  postCash(data, callback){
    var param = {
      url: 'Api/Agent/Cash',
      data: data,
      type: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }


  /*信息详情*/
  getMyInfo(key, callback) {
    var url = 'Api/Agent/mycenter/session_key/' + key;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  /*我的帖子*/
  getmyArticle(key, callback) {
    var url = 'Api/Agent/myArticle/session_key/' + key;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  /*系统消息*/
  getSystemMsg(key, callback) {
    var url = 'Api/Agent/systemMsg/session_key/' + key;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

  /*已采纳的帖子*/
  getadoptArticle(key, callback) {
    var url = 'Api/Agent/adoptArticle/session_key/' + key;
    var param = {
      url: url,
      sCallback: function (data) {
        callback && callback(data);
      }
    };
    this.request(param);
  }

 upload(path,callback) {
   wx.showToast({
     icon: "loading",
     title: "正在上传"
   }),
    wx.uploadFile({
      url: Config.restUrl + "Api/File/upload",
      filePath: path,
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      success: function (res) {
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }

        var data = JSON.parse(res.data);
        var url = Config.baseUrl + data.url;

        callback && callback(url);

      },
      fail: function (e) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast
      }
    })

 }





};

export { Home };